<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>1817. 查找用户活跃分钟数</title>
  </head>

<body>
  <!-- start -->
  <div>https://leetcode.cn/problems/finding-the-users-active-minutes/description/</div>
  <!-- end -->
</body>
<!-- js start -->
<script type="text/javascript">
  /**
   * @param {number[][]} logs
   * @param {number} k
   * @return {number[]}
   */
  var findingUsersActiveMinutes = function (logs, k) {
    const resMap = {};

    const useMap = {};
    logs.map(([userId, time]) => {
      if (useMap[userId]) {
        if (!useMap[userId].includes(time)) useMap[userId].push(time);
      } else {
        useMap[userId] = [time];
      }
    });

    Object.keys(useMap).map((key) => {
      const list = useMap[key];
      let itemTimer = list.length;
      if (!resMap[itemTimer]) {
        resMap[itemTimer] = 1;
      } else {
        resMap[itemTimer] += 1;
      }
    });

    const endNum = Math.max(...Object.keys(resMap));
    const startNum = k > endNum ? 0 : endNum - k;
    const resArr = new Array(k).fill(0).map(function (item, index) {
      let value = resMap[startNum + 1 + index] || 0;
      return value
    });

    return resArr;
  };

  // const res = findingUsersActiveMinutes(
  //   [
  //     [0, 5],
  //     [1, 2],
  //     [0, 2],
  //     [0, 5],
  //     [1, 3],
  //   ],
  //   5
  // );

  // const res = findingUsersActiveMinutes(
  //   // [[305589003,4136],[305589004,4139],[305589004,4141],[305589004,4137],[305589001,4139],[305589001,4139]],
  //   [[3, 4136], [4, 4139], [4, 4141], [4, 4137], [1, 4139], [1, 4139]],
  //   6
  // );

  const res = findingUsersActiveMinutes(
    [[283268890, 14532], [283268891, 14530], [283268889, 14530], [283268892, 14530], [283268890, 14531]]
    , 2
  );

  console.log(res);
</script>

</html>
